package com.junsi.app.controller;

import java.util.List;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.junsi.app.common.util.JwtUtil;
import com.junsi.common.core.controller.BaseController;
import com.junsi.common.core.domain.AjaxResult;
import com.junsi.common.core.page.TableDataInfo;
import com.junsi.demo.domain.ApiDemo;
import com.junsi.demo.service.IApiDemoService;
import com.junsi.ding.talk.util.DingTalkClientUtil;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/**
 * api测试用例 Controller
 * 
 * @author junsi
 * @date 2019-08-11
 */
@Api("api测试用例")
@RestController
@RequestMapping("/demo/apiDemo")
public class ApiDemoController extends BaseController
{
	
	@Autowired
	private IApiDemoService apiDemoService;
	
	/**
	 * 查询api测试用例列表
	 */
    @ApiOperation("获取列表")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(ApiDemo apiDemo)
	{
		startPage();
        List<ApiDemo> list = apiDemoService.selectApiDemoList(apiDemo);
		return getDataTable(list);
	}
	
	/**
	 * 新增保存api测试用例
	 */
    @ApiOperation("新增保存api测试用例")
//    @ApiImplicitParam(name = "ApiDemo", value = "测试用例", dataType = "ApiDemo")
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(ApiDemo apiDemo)
	{
		return toAjax(apiDemoService.insertApiDemo(apiDemo));
	}

	/**
	 * 修改保存api测试用例
	 */
    @ApiOperation("修改保存api测试用例")
//    @ApiImplicitParam(name = "ApiDemo", value = "测试用例", dataType = "ApiDemo")
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(ApiDemo apiDemo)
	{
		return toAjax(apiDemoService.updateApiDemo(apiDemo));
	}
	
	/**
	 * 删除api测试用例
	 */
    @ApiOperation("删除保存api测试用例")
//    @ApiImplicitParam(name = "ApiDemo", value = "测试用例", dataType = "ApiDemo")
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{
		return toAjax(apiDemoService.deleteApiDemoByIds(ids));
	}
    
    /**
     * 单个参数
     */
    @ApiOperation("单个参数")
    @ApiImplicitParam(name = "code", value = "编码", paramType="form", dataType = "String")
    @PostMapping( "/param")
    @ResponseBody
    public String param(String code)
    {
    	SecurityUtils.getSubject().checkRoles("", "", "");
    	String user = "";
    	if(SecurityUtils.getSubject().getPrincipal()==null) {
    		user = "还没有登录，无法获取用户信息";
    	} else {
    		user = JwtUtil.getUsername(SecurityUtils.getSubject().getPrincipal().toString());
    	}
    	return "输入的参数是：code=" + code + ",user=" + user;
    }
    
    /**
     * 多个参数
     */
    @ApiOperation("多个参数")
    @ApiImplicitParams({
    	@ApiImplicitParam(name = "code", value = "编码", paramType="form", dataType = "String", required=true),
    	@ApiImplicitParam(name = "name", value = "名称", paramType="form", dataType = "String")
    })
    @PostMapping( "/params")
    @ResponseBody
    public String params(String code, String name)
    {
    	String user = "";
    	if(SecurityUtils.getSubject().getPrincipal()==null) {
    		user = "还没有登录，无法获取用户信息";
    	} else {
    		user = JwtUtil.getUsername(SecurityUtils.getSubject().getPrincipal().toString());
    	}
    	return "输入的参数是：code=" + code + ",name=" + name + ",user=" + user;
    }

    /**
     * 权限验证例子(已授权)
     */
    @ApiOperation("权限验证例子(已授权)")
    @PostMapping( "/requiresPermissions")
    @ResponseBody
    @RequiresPermissions("apiDemo:requiresPermissions")
    public String requiresPermissions()
    {
		String user = JwtUtil.getUsername(SecurityUtils.getSubject().getPrincipal().toString());
    	return "权限验证例子(已授权)，已登录并且有此操作权限： user=" + user;
    }
    
    /**
     * 权限验证例子(未授权)
     */
    @ApiOperation("权限验证例子(未授权)")
    @PostMapping( "/requiresPermissionsNot")
    @ResponseBody
    @RequiresPermissions("apiDemo:requiresPermissionsNot")
    public String requiresPermissionsNot()
    {
    	String user = JwtUtil.getUsername(SecurityUtils.getSubject().getPrincipal().toString());
    	return "权限验证例子(未授权)，已登录并且有此操作权限： user=" + user;
    }
    
    /**
     * 登录认证例子
     */
    @ApiOperation("登录认证例子")
    @PostMapping( "/requiresAuthentication")
    @ResponseBody
	@RequiresAuthentication
    public String requiresAuthentication()
    {
    	String user = JwtUtil.getUsername(SecurityUtils.getSubject().getPrincipal().toString());
    	return "登录认证例子，已经登录成功： user=" + user;
    }
    
    /**
     * 角色校验例子
     */
    @ApiOperation("角色校验例子")
    @PostMapping( "/hasRole")
    @ResponseBody
    @RequiresAuthentication
    public String hasRole(String role)
    {
    	String user = JwtUtil.getUsername(SecurityUtils.getSubject().getPrincipal().toString());
    	if(role != null && SecurityUtils.getSubject().hasRole(role)){
        	return "角色校验例子，有此角色：role=" + role + "，已经登录成功： user=" + user;
    	}
    	return "角色校验例子，无此角色：role=" + role + "，已经登录成功： user=" + user;
    }
    
    /**
     * 权限校验例子
     */
    @ApiOperation("权限校验例子")
    @PostMapping( "/isPermitted")
    @ResponseBody
    @RequiresAuthentication
    public String isPermitted(String perm)
    {
    	String user = JwtUtil.getUsername(SecurityUtils.getSubject().getPrincipal().toString());
    	if(perm!=null && SecurityUtils.getSubject().isPermitted(perm)){
    		return "权限校验例子，有此权限：permission=" + perm + "，已经登录成功： user=" + user;
    	}
    	return "权限校验例子，无此权限：permission=" + perm + "，已经登录成功： user=" + user;
    }

    /**
     * 发送钉钉消息例子
     */
    @ApiOperation("发送钉钉消息例子")
    @PostMapping( "/sendMessage")
    @ResponseBody
    public String sendMessage()
    {
    	String touser = "manager555";
//    	touser = "010461323824269578";
		String toparty = null;
		String title = "测试标题";
		String content = "测试消息内容";
		DingTalkClientUtil.sendMessageMarkdown(touser, toparty, title, content);
    	return "成功";
    }
    
}
